package org.jeecg.modules.demo.dtwlwjc.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Select;
import org.jeecg.modules.demo.dtwlwjc.entity.TFSensorSoil;
import org.jeecg.modules.demo.dtwlwjc.entity.TFSensorWeather;
import org.jeecg.modules.demo.dtwlwjc.entity.ZwglAlarmSetting;

import java.util.List;
@Mapper
@DS("multi-datasource1")
public interface TFSensorWeatherMapper extends BaseMapper<TFSensorWeather> {
    @Select("SELECT t1.* FROM t_f_sensor_weather t1 " +
            "INNER JOIN (" +
            "    SELECT DATE(synctime) AS date, MAX(id) AS max_id " +
            "    FROM t_f_sensor_weather " +
            "    WHERE synctime BETWEEN #{kaishishijian} AND #{jieshushijian} " +
            "    AND deviceid = #{deviceid} " +
            "    GROUP BY DATE(synctime)" +
            ") t2 ON t1.id = t2.max_id")
    List<TFSensorWeather> selectByCondition(@Param("kaishishijian") String kaishishijian,
                                            @Param("jieshushijian") String jieshushijian,
                                            @Param("deviceid") Integer deviceid);


    @Select("SELECT DATE(synctime) AS date, " +
            "MAX(id) AS id, " +
            "MAX(currainfall) AS currainfall, " +
            "MAX(deviceid) AS deviceid, " +
            "MAX(hum) AS hum, " +
            "MAX(kpa) AS kpa, " +
            "MAX(lux) AS lux, " +
            "MAX(ppm) AS ppm, " +
            "MAX(rainfall) AS rainfall, " +
            "MAX(synctime) AS synctime, " +
            "MAX(tem) AS tem, " +
            "MAX(wind) AS wind, " +
            "MAX(winds) AS winds, " +
            "MAX(dayrainfall) AS dayrainfall " +
            "FROM t_f_sensor_weather " +
            "WHERE deviceid = #{deviceid} " +
            "AND synctime <= (SELECT MAX(synctime) FROM t_f_sensor_weather WHERE deviceid = #{deviceid}) " +
            "AND synctime >= (SELECT DATE_SUB((SELECT MAX(synctime) FROM t_f_sensor_weather WHERE deviceid = #{deviceid}), INTERVAL 7 DAY)) " +
            "GROUP BY DATE(synctime) " +
            "ORDER BY synctime DESC")
    List<TFSensorWeather> getLatestDataForDevice(@Param("deviceid") String deviceid);
}
